import pygame
from .. import constants as C
from .. import setup
from .. import tools
import math


class Box(pygame.sprite.Sprite):
    def __init__(self, x, y, box_type):
        pygame.sprite.Sprite.__init__(self)
        self.x = x
        self.y = y
        self.box_type = box_type
        self.frame_rects = {
            (384, 0, 16, 16),
            (400, 0, 16, 16),
            (416, 0, 16, 16),
            (432, 0, 16, 16)
        }

        self.frames = []
        for frame_rect in self.frame_rects:
            self.frames.append(tools.get_image(setup.GRAPHICS['tile_set'], *frame_rect, (0, 0, 0), C.BRICK_MULTI))
        self.frame_index = 0
        self.image = self.frames[self.frame_index]
        self.rect = self.image.get_rect()
        self.rect.x = self.x
        self.rect.y = self.y

        # 初始化透明度和计时器
        self.alpha = 255
        self.alpha_direction = -5  # 透明度变化方向，-5表示透明度逐渐减小
        self.alpha_timer = 0

    def update(self):
        # 更新透明度
        self.alpha_timer += 1
        if self.alpha_timer % 2 == 0:  # 每10帧更新一次透明度
            self.alpha += self.alpha_direction
            if self.alpha <= 155:  # 设置最小透明度
                self.alpha_direction = 5  # 变回增加透明度
            elif self.alpha >= 255:  # 设置最大透明度
                self.alpha_direction = -5  # 变回减少透明度

        # 应用透明度
        self.image.set_alpha(self.alpha)
